<template>
    <div class="page-background">
        <div class="login-container">
            <el-form :rules="easyrules" ref="formRef" :model="loginObj">
                <h1>登陆界面</h1>
                <el-form-item prop="phoneNumber">
                    <el-input v-model="loginObj.phoneNumber" placeholder="电话号码" class="login-input" />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginObj.password" placeholder="密码" class="login-input" />
                </el-form-item>
                <el-form-item prop="code">
                    <el-row class="login-row">
                        <el-col :span="10" class="input-col">
                            <el-input v-model="loginObj.code" placeholder="请输入验证码"
                                class="login-input captcha-input"></el-input>
                        </el-col>
                        <el-col :span="5" class="captcha-col">
                            <img :src="captchaImage" @click="refreshCaptcha" alt="Captcha" id="checkImg"
                                class="captcha-img" />
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item prop="remember">
                    <el-checkbox v-model="autoLogin">自动登录</el-checkbox>
                    <el-checkbox v-model="remember">记住我</el-checkbox>
                </el-form-item>
                <el-button type="primary" @click="dologin()">提交</el-button>
            </el-form>
        </div>
    </div>





</template>

<script setup>

import { onMounted, ref, watch } from 'vue'
import easyapi from '../api'

import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

onMounted(async function () {
    refreshCaptcha(); // 初始加载验证码
    beforeRouteEnter()
    if (localStorage.getItem("autoLogin") === "true") {
        ElMessage({
            type: 'success',
            message: '自动登陆成功',
        })
        setTimeout(() => {
            //跳转页面
            //js代码跳转
            router.push('/charts');
        }, 500)
    } else if(localStorage.getItem("REMEMBER_KEY") === "true"){
        loginObj.value.phoneNumber = localStorage.getItem("phoneNumber")
        loginObj.value.password = localStorage.getItem("password")
    }
})

const beforeRouteEnter = (to, from, next) => {

    document.querySelector('body').setAttribute('style', 'margin:0;padding:0')


}

const router = useRouter()

const formRef = ref({})

const autoLogin = ref(false)
const remember = ref(false)

watch(autoLogin, (newVal) => {
    if (newVal) {
        remember.value = true;
    }
});

watch(remember, (newVal) => {
    if (!newVal) {
        autoLogin.value = false;
    }
});


const loginObj = ref({})
const dologin = async () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let result = await easyapi.postJson('/api/dologin', loginObj.value)
            if (result.code == 200 && result.data) {
                localStorage.setItem("token", result.data)
                localStorage.setItem("REMEMBER_KEY", remember.value)
                if (remember.value) {
                    localStorage.setItem("phoneNumber", loginObj.value.phoneNumber)
                    localStorage.setItem("password", loginObj.value.password)
                    if(autoLogin.value){
                        localStorage.setItem("autoLogin", autoLogin.value)
                    }
                }
                //提示
                ElMessage({
                    type: 'success',
                    message: '登录成功',
                })
                setTimeout(() => {
                    //跳转页面
                    //js代码跳转
                    router.push('/main');
                }, 1000)
            } else {
                refreshCaptcha()
                ElMessage({
                    type: 'info',
                    message: result.message,
                })
            }
        }
    })
}





const checkpass = (rule, value, callback) => {
    let regex = /^.{6,16}$/;
    if (regex.test(loginObj.value.password)) {
        callback();
    } else {
        callback(new Error('密码长度在6-16之间'))
    }
}

const easyrules = {
    phoneNumber: [
        { required: true, message: '请输入电话号码', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { validator: checkpass, trigger: 'blur' }
    ],
    code: [
        { required: true, message: '请输入验证码', trigger: 'blur' },
    ]
};

//验证码
const captchaImage = ref('');

const refreshCaptcha = async () => {
    let response = await easyapi.get('/api/captcha');
    console.log(response.data)
    captchaImage.value = response.data.image
    loginObj.value.key = response.data.key
}



</script>

<style scoped>
.page-background {
    background: url('../img/mount.png') no-repeat center / cover;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-container {
    width: 100%;
    max-width: 400px;
    margin: 100px auto;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background-color: #fff;
    text-align: center;
    background-image: url('');
    /* 添加背景图片 */
    background-size: cover;
    /* 背景图片覆盖整个容器 */
    background-position: center;
    /* 背景图片居中显示 */
}

.login-container h1 {
    margin-bottom: 20px;
    color: #333;
}

.login-input {
    width: 100%;
    margin-bottom: 15px;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
    /* 确保padding和border不会增加元素的总宽度 */
}

.el-button--primary {
    width: 100%;
    padding: 10px;
    background-color: #409EFF;
    border-color: #409EFF;
    color: #fff;
    border-radius: 4px;
    cursor: pointer;
}

.el-button--primary:hover {
    background-color: #66b1ff;
    border-color: #66b1ff;
}


/* 美化验证码输入框 */
.captcha-input {
    border: 1px solid #ccc;
    /* 更改边框颜色和宽度 */
    border-radius: 6px;
    /* 更改边框圆角 */
    transition: border-color 0.3s;
    /* 添加边框颜色过渡效果 */
}

.captcha-input:focus {
    border-color: #66b1ff;
    /* 聚焦时更改边框颜色 */
}

/* 美化验证码图片 */
.captcha-img {
    size: 100%;
    border: 1px solid #ccc;
    /* 添加边框 */
    border-radius: 4px;
    /* 更改边框圆角 */
    cursor: pointer;
    /* 鼠标悬停时显示手形图标 */
    transition: border-color 0.3s;
    margin-left: 35px;
}

.captcha-img:hover {
    border-color: #409EFF;
    /* 鼠标悬停时更改边框颜色 */
}

.login-row {
    display: flex;
    align-items: center;
    /* 垂直居中 */
}
</style>